import StatusNav from '@/components/school/statusNav';
import PageBody from '@/components/school/pageBody';
import {mapGetters} from "vuex";
import * as ProductApi from '@/api/school/product.js';
import diyJson from '@/config/diy.js';

export const mixins =
    {
      computed: mapGetters(['uid', 'userInfo', 'shareInfo', 'schoolId']),
      components: {
        StatusNav,
        PageBody,
      },
      data() {
        return {
          // #ifdef H5
          stickyTop: 0,
          // #endif
          // #ifdef MP
          stickyTop: 44,
          // #endif
          statusNavProperties: { // 筛选条件
            title: "跳骚市场",
            setStyle: diyJson.statusbar.setStyle,
            showLeft:true,
            showHome:true
          },
          themConfig: diyJson.commonStyle,
          // 轮播
          swiperImgs: ['https://minio.thisforyou.cn:1443/school/banner/ershoubanner.jpeg'],
          page: {
            detail: "/pages/school/product-idle/detail/index",
          },

          //  是否授权
          isAuth: false,

          // 系统设备的一些宽高参数
          window: {},
          tabActive: 0,
          product: {
            list: [],
            page: {
              pageSize: 20,
              pageNum: 1,
              pages: 1,
            },

            typeList: [],

          },

          notice: '闲置转手，线下面对面交易，近距离，不邮寄，效率快',
          searchParams: {
            typeNo: '',
            hamlet: ''
          },
          isReachBottom: false,
          loading: {
            status: 'nomore',
            loadingText: '加载中',
            loadmoreText: '点击加载更多',
          },
          dataPage: {
            nextId: undefined,
            hasNext: false,
            ext: undefined
          },
          schoolInfo: {
            schoolId: '',
            schoolName: '',
          }
        };
      },

      onLoad: function (options) {
        if (this.userInfo.schoolInfo) {
          this.schoolInfo.schoolId = this.userInfo.schoolInfo.schoolId
          this.schoolInfo.schoolName = this.userInfo.schoolInfo.schoolName
        }
        this.getProductData();
        this.getTypeList();
      },

      onShow() {

      },

      methods: {
        getTypeList() {
          ProductApi.typeList().then(res => {
            let all = {name: "全部", value: ''}
            let arr = [];
            arr.push(all);
            res.data.forEach(e => {
              arr.push(e)
            })
            this.product.typeList = arr;
          })
        }
        ,

        tabOnChange(e) {
          let index = e.index
          let type = this.product.typeList[index];
          let value = type.value;
          if (index === 0) {
            value = '';
          }
          this.searchParams.typeNo = value;
          this.getProductData()
        }
        ,
        toDetail(item) {
          uni.navigateTo({
            url: this.page.detail + "?productNo=" + item.productNo
          })
        }
        ,
        // 切换学校
        switchSchoolTab() {
          uni.navigateTo({
            url: '/pages/school/info/index'
          })
        },
        setSchool(school) {
          this.schoolInfo.schoolId = school.id
          this.schoolInfo.schoolName = school.schoolName
          this.getProductData();
        },

        getProductData() {
          ProductApi.productList(
              {...this.searchParams, schoolId: this.schoolInfo.schoolId}).then(
              res => {
                this.product.list = res.data.records;
                this.dataPage.hasNext = res.data.hasNext;
                if (res.data.nextId) {
                  this.dataPage.nextId = res.data.nextId;
                }
              })
        }
        ,

        scrolltolower() {
          this.isReachBottom = true;
          this.loading.status = 'nomore';
          if (this.dataPage.hasNext) {
            this.loading.status = 'loadmore';
          }
        },
        loadmore() {
          if (this.dataPage.hasNext) {
            this.loading.status = 'loading';
            ProductApi.productList({
              ...this.searchParams,
              schoolId: this.schoolInfo.schoolId,
              nextId: this.dataPage.nextId
            }).then(
                res => {
                  res.data.records.forEach(item => {
                    this.product.list.push(item)
                  })

                  this.dataPage.hasNext = res.data.hasNext;
                  if (res.data.nextId) {
                    this.dataPage.nextId = res.data.nextId;
                  }
                  if (res.data.hasNext) {
                    this.loading.status = 'loadmore';
                  } else {
                    this.loading.status = 'nomore';
                  }
                })
          }
        }

      }
      ,
      onReachBottom() {
        this.scrolltolower();
      },
      onShareAppMessage() {
        return {
          title: '跳骚市场',
          path: 'pages/school/product-idle/index',
          imageUrl: '' // 自定义分享图片（选填）
        }
      }
    }
